<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">社团管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>社团列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  输入框组-->
    <div>
      <br/>
      <span>社团名称 &nbsp;&nbsp;</span>
      <el-input
          placeholder="请输入社团名称"
          prefix-icon="el-icon-search"
          v-model="name" class="inputMax" >
      </el-input>
      <span>&nbsp;&nbsp;社团类型&nbsp;&nbsp;</span>
      <el-select v-model="clubType" placeholder="请选择" class="inputMax">
        <el-option
            v-for="item in clubTypes"
            :key="item.id"
            :label="item.name"
            :value="item.name">
        </el-option>
      </el-select>
      &nbsp;&nbsp;
      <el-button type="primary" icon="el-icon-search" @click="clubSearch" ><span>查询</span></el-button>
      <el-button type="success" icon="el-icon-plus" @click="toClubAdd">添加</el-button>
      <br/>
      <br/>
    </div>
    <el-table
        :data="clubs"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="社团编号"
          width="180">
      </el-table-column>
      <el-table-column
          prop="name"
          label="社团名称"
          width="100">
      </el-table-column>
      <el-table-column
          prop="type"
          label="社团类型">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | DateFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column
          fixed="right"
          width="180"
          label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="toClubById(scope.row.id)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="open(scope.row.id) ">删除</el-button>
        </template>

      </el-table-column>
    </el-table>

    <el-pagination
        background
        layout="prev, pager, next"
        :page-size="pageSize"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
import {clubTypeList, clubList, clubDelete} from '@/api'
export default {
  name: "ClubList",
  components: {
  },
  data() {
    return {
      name: '',
      clubTypes: [],
      clubType: '',
      clubs: [],
      total: 0,
      pageSize: 0,
    }
  },
  created() {
    clubTypeList().then(result=>{
      this.clubTypes = result.data;
    });
    clubList().then(result=>{
      this.clubs = result.data.list;
      this.total = result.data.total;
      this.pageSize = result.data.pageSize;
    })
  },
  methods: {
    clubSearch(){
      let club = {
        name: this.name,
        type: this.clubType,
      };
      clubList(club).then(result=>{
        this.clubs = result.data.list;
      })
    },

    toClubAdd(){
      this.$router.push('/club/add')
    },

    toClubById(id){
      this.$router.push({
        path:'/club/add',
        query: {
          id: id,
        }
      })
    },

    open(id) {
      this.$confirm('此操作将永久删除该社团, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let club = {
          id: id,
        }
         clubDelete(club).then(()=>{
           clubList().then(result=>{
             this.clubs = result.data.list;
             this.total = result.data.total;
             this.pageSize = result.data.pageSize;
           })
         });
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  }
}
</script>

<style scoped>
.inputMax{
  width: 200px;
}
</style>
